const stringifyForLog = (value, max = 800) => {
  if (value === undefined || value === null) {
    return value;
  }
  if (typeof value === 'string') {
    return value.length > max ? `${value.slice(0, max)}...` : value;
  }
  try {
    const str = JSON.stringify(value);
    return str.length > max ? `${str.slice(0, max)}...` : str;
  } catch (error) {
    return String(value);
  }
};

import { createDefaultTool } from './default-tool.js';

const baseTool = createDefaultTool('chrome_fill_or_select');

export const chromeFillOrSelectTool = {
  ...baseTool,
  async beforeCall({ baseAgent, args }) {
    console.log('[Tools][chrome_fill_or_select] beforeCall args', stringifyForLog(args));
    if (args?.selector && typeof baseAgent.ensureInputIsActive === 'function') {
      const result = await baseAgent.ensureInputIsActive(args.selector);
      console.log('[Tools][chrome_fill_or_select] beforeCall result', stringifyForLog(result));
      return result;
    }
    return null;
  },
  suppressObservation: true,
  skipDelay: true
};

